/**
 * *******************************************
 * user: zmq
 * describe: gate服务器 负责分配路由给connector
 * created:
 * date: 2021/8/18
 * *******************************************
 */

import {Application, FrontendSession} from "pinus";
import gateService from "../../../service/gateService";
import {ILoginAccount, IRegisterAccount} from "../../../consts/interface";
import Dispatcher from "../../../util/dispatcher";


export default function (app: Application) {

    return new GateHandler(app);
}

/**
 * 创建一个 GateHandler类        不参与逻辑处理，逻辑处理交给gateService
 */
export class GateHandler {

    /**
     * 构造函数
     * @param {Application} app
     */
    constructor(private app: Application) {

    }

    /**
     * 账号注册
     * @param msg
     * @param {FrontendSession} session
     * @returns {Promise<any>}
     */
    public async accountRegister(msg: any, session: FrontendSession): Promise<any> {

        let sessionId: number = session.id;        // 获取session id
        let clientAddress = this.app.get('sessionService').getClientAddressBySessionId(sessionId); // 连接地址 包含IP 和端口
        let registerMsg : IRegisterAccount = {
            account: msg.account,
            pwd: msg.pwd,
            clientAddress: clientAddress
        };

        // gateService的accountRegister进行账号注册，并返回结果
        return await gateService.accountRegister(registerMsg);
    }


    /**
     * 账号登录
     * @param msg
     * @param {FrontendSession} session
     * @returns {Promise<any>}
     */
    public async accountLogin(msg: any, session: FrontendSession): Promise<any> {

        let sessionId: number = session.id;        // 获取session id
        let clientAddress = this.app.get('sessionService').getClientAddressBySessionId(sessionId); // 连接地址 包含IP 和端口
        let loginMsg : ILoginAccount = {            // 声明接口 并赋值
            account: msg.account,
            pwd: msg.pwd,
            clientAddress: clientAddress
        };

        // gateService的accountLogin进行账号登录操作，并返回结果
        return await gateService.accountLogin(session,loginMsg);
    }


    /**
     *  客户端通过route与该函数交互  查询connector服务器的入口
     * @param  {Object}   msg     request message
     * @param  {Object}   session current session object
     */
    public async queryEntry(msg: any, session: FrontendSession): Promise<any> {

        // 调用app.getServ ersByType(‘connector’)获得所有的connector服务器，connectors是一个connector数组
        // 当前存在三种类型的服务器gate，connector和chat ，具体服务器的配置是位于game-server\config\servers.json

        let connectors = this.app.getServersByType('connector');
        if (!connectors || connectors.length === 0) {       // 校验下connector的合法性
            return {code: 500, msg: ""};
        }

        msg.account = "2";
        msg.pwd = "1";
        let data = await this.accountLogin(msg, session);
        console.log("data ----->>>>>>   " , data);

        // 根据用户名uid和connectors数组通过功能函数dispatch去分配一个connector服务器
        let res = Dispatcher.dispatch("12aaa", connectors);

        // 返回给客户端
        return {host: res.host, port: res.clientPort}
    }

}